import Api from '@/api';
export default {
    state: {
        token: null,
        userInfo: null,
    },
    mutations: {
        setToken(state, value) {
            state.token = value;
        },
        setUserInfo(state, value) {
            state.userInfo = value;
        },
    },
    actions: {
        // 设置token
        async token({ commit }, obj) {
            uni.setStorageSync('token', obj.accessToken);
            commit('setToken', obj.accessToken);
        },
        // 获取用户信息
        async getUserInfo({ commit }) {
            return new Promise((resolve, reject) => {
                Api.member.user
                    .get()
                    .then(res => {
                        commit('setUserInfo', res);
                        uni.setStorageSync('userInfo', res);
                        resolve(res);
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },
        async clearToken({ commit }) {
            commit('setToken', null);
        },
        async clearUserInfo({ commit }) {
            commit('setUserInfo', null);
        },
    },
};
